package gogogo.liwushuo.fragment;


import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import gogogo.liwushuo.R;
import gogogo.liwushuo.activity.SearchActivity;
import gogogo.liwushuo.activity.SignInActivity;
import gogogo.liwushuo.adapter.GridViewAdapter;
import gogogo.liwushuo.adapter.VpAdapter;
import gogogo.liwushuo.app.IApp;
import gogogo.liwushuo.bean.ScollerBean;
import gogogo.liwushuo.constant.Resource;
import gogogo.liwushuo.util.OKHttpUtils;

public class HomeFragment extends Fragment {


    @InjectView(R.id.scollerview_iv)
    ImageView scollerviewIv;
    @InjectView(R.id.llayout)
    LinearLayout llayout;
    @InjectView(R.id.signin)
    ImageView signin;
    @InjectView(R.id.search)
    ImageView search;
    private PopupWindow popupWindow;
    private GridView gridView;

    public static HomeFragment newInstance() {

        Bundle args = new Bundle();

        HomeFragment fragment = new HomeFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @InjectView(R.id.toolbar_home)
    Toolbar toolbarHome;
    @InjectView(R.id.viewpager)
    ViewPager viewpager;
    @InjectView(R.id.tblayout)
    TabLayout tblayout;

    private static final String TAG = "mtag";
    private OKHttpUtils okHttpUtils;
    private Context mContext;
    private String jsonDataScollerView;
    private ScollerBean scollerBean;
    private List<ScollerBean.DataBean.ChannelsBean> channels;
    private List<SubHomeFragment> list;
    private boolean isDown;
    private int hideIndex;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        ButterKnife.inject(this, view);
        signin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), SignInActivity.class));
            }
        });
        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), SearchActivity.class));
            }
        });
        return view;
    }

    private void initPopWindow() {
        View popView = LayoutInflater.from(mContext).inflate(R.layout.home_pop, llayout, false);
        gridView = (GridView) popView.findViewById(R.id.gridview);
        gridView.setAdapter(new GridViewAdapter(channels, mContext));
        popupWindow = new PopupWindow(llayout, IApp.mScreenWidth, IApp.mScreenHeight / 3);
        popupWindow.setContentView(popView);
        popupWindow.setOutsideTouchable(true);
//        popupWindow.setFocusable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.showAsDropDown(llayout);
        popupWindow.dismiss();
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                showLine(position, hideIndex);
                viewpager.setCurrentItem(position);
            }
        });
        scollerviewIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (isDown) {
                    scollerviewIv.setImageResource(R.mipmap.arrow_index_up);
                    isDown = !isDown;
                    popupWindow.dismiss();
                } else {
                    scollerviewIv.setImageResource(R.mipmap.arrow_index_down);
                    isDown = !isDown;
//                    initPopWindow();
                    popupWindow.showAsDropDown(llayout);
                    int currentItem = viewpager.getCurrentItem();
                    if (gridView.getChildAt(currentItem) != null) {
                        showLine(currentItem, hideIndex);
                    }

//                    Log.i(TAG, "onClick: "+currentItem);

                }

            }
        });
//        popupWindow.setWidth(IApp.mScreenWidth);
//        popupWindow.setHeight(IApp.mScreenHeight);

    }

    private void initData() {
        mContext = getActivity();
        okHttpUtils = OKHttpUtils.newInstance(mContext);
        okHttpUtils.getAsyncMainData(Resource.URL_HORIZTSCOLLVIEW, new OKHttpUtils.MyCallBack() {
            @Override
            public void onFailure(IOException e) {
            }

            @Override
            public void onResponse(String result) {
                jsonDataScollerView = result;
                scollerBean = new Gson().fromJson(jsonDataScollerView, ScollerBean.class);
                channels = scollerBean.getData().getChannels();
                initFragmentList();
                initPopWindow();
            }
        });
    }

    private void initFragmentList() {
        list = new ArrayList<>();
        for (int i = 0; i < channels.size(); i++) {
            list.add(SubHomeFragment.newInstance(i));
        }
        viewpager.setAdapter(new VpAdapter(getChildFragmentManager(), list));
        tblayout.setupWithViewPager(viewpager);
        viewpager.setOffscreenPageLimit(1);
        for (int i = 0; i < list.size(); i++) {
            TabLayout.Tab tab = tblayout.getTabAt(i);
            tab.setText(channels.get(i).getName());
        }

    }

    private void showLine(int showIndex, int hideIndex) {
        View showview = gridView.getChildAt(showIndex);
        TextView showline = (TextView) showview.findViewById(R.id.tv_line);
        if (showIndex != hideIndex) {
            View hideview = gridView.getChildAt(hideIndex);
            TextView hideline = (TextView) hideview.findViewById(R.id.tv_line);
            showline.setVisibility(View.VISIBLE);
            hideline.setVisibility(View.GONE);
            this.hideIndex = showIndex;
        } else {
            showline.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        toolbarHome.setTitle("");
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
//        setListener();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }
}
